package com.jinlia.show.modules.wxapp.controller;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.model.form.OrderForm;
import com.jinlia.show.modules.base.wxapp.entity.ShoppingCart;
import com.jinlia.show.modules.base.model.query.ShoppingCartQuery;
import com.jinlia.show.modules.base.wxapp.service.ShoppingCartMapperService;
import com.jinlia.show.modules.wxapp.model.form.PreOrderForm;
import com.jinlia.show.modules.wxapp.model.vo.OrderVO;
import com.jinlia.show.modules.base.model.vo.ShoppingCartBillVO;
import com.jinlia.show.modules.wxapp.model.vo.ShoppingCartVO;
import com.jinlia.show.modules.wxapp.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 映射前端basket 购物车
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/shopping/cart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     *
     * @return
     */
    @PostMapping("add")
    public ShoppingCartBillVO add(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setUserId(UserInfoUtils.getUserId());
        return shoppingCartMapperService.addShoppingCart(shoppingCart);
    }

    /**
     * 删除购物车
     */
    @PostMapping("del")
    public Boolean del(@RequestBody OrderForm shoppingCart) {
        return shoppingCartService.shoppingCartService(shoppingCart.getShoppingCartIds());
    }

    /**
     * 获取购物车
     */
    @PostMapping("get")
    public List<ShoppingCartVO> getVO(@RequestBody ShoppingCartQuery shoppingCartQuery) {
        shoppingCartQuery.setUserId(UserInfoUtils.getUserId());
        shoppingCartQuery.setEnableStatus(EnableStatusEnum.ENABLE.getValue());
        return shoppingCartService.getVO(shoppingCartQuery);
    }

    /**
     * 更新购物车
     */
    @PostMapping("update/num")
    public ShoppingCart updateNum(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartMapperService.updateShoppingCartNum(shoppingCart);
    }


    /**
     * 预计算价格
     */
    @PostMapping("/calculate")
    public OrderVO calculateMoney(@RequestBody PreOrderForm preOrderForm) {
        return shoppingCartService.calculateMoney(preOrderForm);
    }

    /**
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("bill")
    public ShoppingCartBillVO get(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setUserId(UserInfoUtils.getUserId());
        return shoppingCartMapperService.bill(shoppingCart);
    }
}
